package com.fafu.control;

import com.fafu.service.StudentRepairService;
import com.fafu.service.StudentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.models.auth.In;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import util.Dictionary;
import util.JsonRsp;
import util.MyMap;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static util.Dictionary.CODE.PARAM_ERROR;
import static util.Dictionary.REPAIR.*;
import static util.Dictionary.REPAIR.REPAIR_STATE_REFUSE;

@Api("学生信息接口")
@RestController
@RequestMapping("/student")
public class StudentControl {
    private Logger logger = LoggerFactory.getLogger(StudentControl.class);
    @Autowired
    private StudentService studentService;
    @Autowired
    private StudentRepairService studentRepairService;
    @ApiOperation("获得学生信息")
    @RequestMapping(value = "/getStudent", method=RequestMethod.POST)
    public JsonRsp studentRepair(
            @ApiParam("学生学号")@RequestParam("studentiden") String studentiden){
        try{
            Map<String,Object> map =  studentService.getStudent(studentiden);
            Integer code = (Integer) map.get("code");
            if(code.equals(Dictionary.CODE.SUCCESS))
                return JsonRsp.success(map.get("data"));
            return JsonRsp.fail(code, (String) map.get("msg"));
        }catch(Exception e){
            logger.error(e.getMessage());
            return JsonRsp.fail(Dictionary.CODE.SYSTEM_ERROR,"系统内部错误");

        }
    }

    @ApiOperation("获得学生信息")
    @RequestMapping(value = "/getStudentById", method=RequestMethod.POST)
    public JsonRsp getStudentById(
            @ApiParam("id")@RequestParam("id") Integer id){
        try{
            Map<String,Object> map =  studentService.getStudentById(id);
            Integer code = (Integer) map.get("code");
            if(code.equals(Dictionary.CODE.SUCCESS))
                return JsonRsp.success(map.get("data"));
            return JsonRsp.fail(code, (String) map.get("msg"));
        }catch(Exception e){
            logger.error(e.getMessage());
            return JsonRsp.fail(Dictionary.CODE.SYSTEM_ERROR,"系统内部错误");

        }
    }

    @ApiOperation("获得学生信息List")
    @RequestMapping(value = "/getStudentList", method=RequestMethod.POST)
    public JsonRsp getStudentList(
            @ApiParam("性别")@RequestParam(value = "gender",required = false) Integer gender
            , @ApiParam("区域")@RequestParam(value = "area",required = false) Integer area
            , @ApiParam("学号")@RequestParam(value = "iden",required = false) String iden
            , @ApiParam("宿舍")@RequestParam(value = "studentAddr",required = false) String studentAddr
            , @ApiParam("是否继续使用")@RequestParam(value = "isUsed",required = false)Integer isUsed
            , @ApiParam("学生姓名")@RequestParam(value = "sName",required = false)String sName
            , @ApiParam("第几页")@RequestParam(value = "pageNum", defaultValue = "1")Integer pageNum
            , @ApiParam("每页几条，默认20")@RequestParam(value = "pageSize",defaultValue = "20")Integer pageSize){
        try{
            Map<String,Object> reMap = studentService.getStudentList(new MyMap()
                    .put("gender",gender)
                    .put("area",area)
                    .put("iden",iden)
                    .put("studentAddr",studentAddr)
                    .put("isUsed",isUsed)
                    .put("pageNum",pageNum)
                    .put("pageSize",pageSize)
                    .put("sName",sName).toMap());
            Integer code = (Integer) reMap.get("code");
            if(code.equals(Dictionary.CODE.SUCCESS))
                return JsonRsp.success(reMap.get("data")).setCount((Long) reMap.get("count"));

            return  JsonRsp.fail(code, (String) reMap.get("msg"));
        }catch(Exception e){
            logger.error(e.getMessage());
            return JsonRsp.fail(Dictionary.CODE.SYSTEM_ERROR,"系统内部错误");

        }
    }

    @ApiOperation("修改学生使用权")
    @RequestMapping(value = "/studentStateChange", method=RequestMethod.POST)
    public JsonRsp studentStateChange(
            @ApiParam("id") @RequestParam(value = "id")Integer id){
        try{
            Map<String,Object> reMap = studentService.stateChange(new MyMap()
                    .put("id",id).toMap());
            Integer code = (Integer) reMap.get("code");
            if(code.equals(Dictionary.CODE.SUCCESS))
                return JsonRsp.success(reMap.get("data"));
            return  JsonRsp.fail(code, (String) reMap.get("msg"));
        }catch(Exception e){
            logger.error(e.getMessage());
            return JsonRsp.fail(Dictionary.CODE.SYSTEM_ERROR,"系统内部错误");

        }
    }
    @ApiOperation("报修查询接口")
    @RequestMapping(value = "/search",method = RequestMethod.GET)
    public JsonRsp search(
            @ApiParam("学生Id") @RequestParam("sid")Long sid
            ,@ApiParam("第几页")@RequestParam(value = "pageNum", defaultValue = "1")Integer pageNum
            ,@ApiParam("每页几条，默认20")@RequestParam(value = "pageSize",defaultValue = "20")Integer pageSize){
        try{
            List<Integer> stateType = new ArrayList<>();
            Map<String,Object> paramMap = new HashMap<>();
            paramMap.put("id",sid);
            paramMap.put("pageNum",pageNum);
            paramMap.put("pageSize",pageSize);
            Map<String,Object> reMap = studentRepairService.searchListAll(paramMap);

            return JsonRsp.success(reMap.get("data")).setCount((Long) reMap.get("count"));

        }catch (Exception e){
            logger.error(e.getMessage());
            return JsonRsp.fail(Dictionary.CODE.SYSTEM_ERROR,"系统内部错误");
        }
    }
}
